package com.fau.cm.action.index;

import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalPreferentialPlan;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc  优惠方案管理Action
 * @author yangzj
 * @datetime 2014年10月16日,上午11:05:20
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class BillingPreferentialAction extends PublicAction implements ModelDriven<MedicalPreferentialPlan>{
	
	private static final long serialVersionUID = 5039198219359365611L;
	
	private MedicalPreferentialPlan preferentialPlan = new MedicalPreferentialPlan();

	/**
	 * @desc 跳转到优惠方案管理页面
	 * @return String
	 * @author yangzj
	 * @datetime 2014年10月16日,上午11:16:04
	 */
	@Action(value = "preferential_scheme_init", results = {@Result(location = "preferential_scheme_init.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入优惠方案添加页面
	 * @return String
	 * @author liy
	 * @datetime 2014年12月10日,下午12:20:59
	 */
	@Action(value = "preferential_scheme_add", results = {@Result(location = "preferential_scheme_add.jsp")})
	public String add(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入优惠方案修改页面
	 * @return String
	 * @author liy
	 * @datetime 2014年12月10日,下午3:23:44
	 */
	@Action(value = "preferential_scheme_edit", results = {@Result(location = "preferential_scheme_edit.jsp")})
	public String edit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存优惠方案
	 * @return null
	 * @author liy
	 * @datetime 2014年12月10日,下午1:31:51
	 */
	@Action(value = "preferential_scheme_save")
	public String save(){
		this.baseAllService.getBillingManagerService().insertPreferentialPlan(this.preferentialPlan);
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		this.map.put("success", true);
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 更新优惠方案
	 * @return String
	 * @author liy
	 * @datetime 2014年12月10日,下午3:40:24
	 */
	@Action(value = "preferential_scheme_update")
	public String update(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(this.preferentialPlan.getId())){
			this.baseAllService.getBillingManagerService().updatePreferentialPlan(this.preferentialPlan);
			this.map.put("success", true);
		}else{
			this.map.put("success", false);
		}
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 分页查询优惠方案
	 * @return null
	 * @author liy
	 * @datetime 2014年12月10日,下午2:27:21
	 */
	@Action(value = "preferential_scheme_list")
	public String page(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getBillingManagerService().findPreferentialPlanByPage(map, preferentialPlan, admin.getEnterpriseId());
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除优惠方案
	 * @return null
	 * @author liy
	 * @datetime 2014年12月10日,下午3:13:34
	 */
	@Action(value = "preferential_scheme_del")
	public String delete(){
		HttpServletResponse response = ServletActionContext.getResponse();
		if(StringUtils.isNotBlank(preferentialPlan.getId())) {
			this.baseAllService.getBillingManagerService().deletePreferentialPlan(this.preferentialPlan);
			this.map = new HashMap<String, Object>();
			this.map.put("success", true);
		} else {
			this.map.put("success", false);
		}
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 查询可用的优惠方案
	 * @return null
	 * @author liy
	 * @datetime 2014年12月10日,下午9:29:51
	 */
	@Action(value = "preferential_scheme_usable")
	public String usable(){
		this.list = this.baseAllService.getBillingManagerService().findUsablePreferentialPlan();
		HttpServletResponse response = ServletActionContext.getResponse();
		formatJSON(response, this.list);
		return null;
	}

	@Override
	public MedicalPreferentialPlan getModel() {
		return preferentialPlan;
	}
}
